package com.woniu.product.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.product.client.CouponManageClient;
import com.woniu.product.client.param.AddCouponLogParram;
import com.woniu.product.client.param.ProvideCouponParam;
import com.woniu.product.client.param.ReceiveCouponParam;
import com.woniu.product.client.param.UpdateCouponLogParam;
import com.woniu.product.service.CouponLogsService;
import com.woniu.product.service.CouponReceiveService;
import com.woniu.product.service.CouponService;
import com.woniu.product.service.form.AddCouponLogServiceParram;
import com.woniu.product.service.form.ProvideCouponServiceParam;
import com.woniu.product.service.form.ReceiveCouponServiceParam;
import com.woniu.product.service.form.UpdateCouponLogServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class CouponManageApiImpl implements CouponManageClient {

    @Resource
    private CouponLogsService couponLogsService;

    @Resource
    private CouponReceiveService couponReceiveService;

    @Resource
    private CouponService couponService;

    /**
     * 发放优惠券
     */
    @Override
    public Result provideCoupon(ProvideCouponParam provideCouponParam) {
        log.info("发放优惠券:{}", provideCouponParam);
        ProvideCouponServiceParam provideCouponServiceParam = BeanUtil.copyProperties(provideCouponParam, ProvideCouponServiceParam.class);
        couponService.provideCoupon(provideCouponServiceParam);
        return Result.getInstance();
    }

    /**
     * 领取优惠券
     *
     * @param receiveCouponParam
     * @return
     */
    @Override
    public Result receiveCoupon(ReceiveCouponParam receiveCouponParam) {
        log.info("领取优惠券:{}", receiveCouponParam);
        ReceiveCouponServiceParam receiveCouponServiceParam = BeanUtil.copyProperties(receiveCouponParam, ReceiveCouponServiceParam.class);
        couponReceiveService.receiveCoupon(receiveCouponServiceParam);
        return Result.getInstance();
    }


    /**
     * 新增优惠券使用记录
     *
     * @param addCouponLogParram
     * @return
     */
    @Override
    public Result addCouponLog(AddCouponLogParram addCouponLogParram) {
        log.info("新增优惠券记录:{}", addCouponLogParram);
        AddCouponLogServiceParram addCouponLogServiceParram = BeanUtil.copyProperties(addCouponLogParram, AddCouponLogServiceParram.class);
        couponLogsService.addCouponLog(addCouponLogServiceParram);
        return Result.getInstance();
    }

    /**
     * 修改优惠券使用记录
     *
     * @param updateCouponLogParam
     * @return
     */
    @Override
    public Result updateCouponLog(UpdateCouponLogParam updateCouponLogParam) {
        log.info("修改优惠券记录:{}", updateCouponLogParam);
        UpdateCouponLogServiceParam updateCouponLogServiceParam = BeanUtil.copyProperties(updateCouponLogParam, UpdateCouponLogServiceParam.class);
        couponLogsService.updateCouponLog(updateCouponLogServiceParam);
        return Result.getInstance();
    }

    /**
     * 修改领取的优惠券使用状态
     *
     * @param couponReceiveId
     * @return
     */
    @Override
    public Result updateCouponReceive(Integer couponReceiveId) {
        log.info("修改领取的优惠券使用状态:{}", couponReceiveId);
        couponReceiveService.updateReceiveRecord(couponReceiveId);
        return Result.getInstance();
    }

}
